package cn.egame.extraction.dao;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.gc.GameTagInfo;

public class GameTagDao extends BaseDao {

	public GameTagDao() throws ExceptionCommonBase {
		super("egame_game");
	}

	public int getGameTagId(String tagName) throws ExceptionCommonBase {
		String sql = "select tag_id  from t_game_tag where tag_name="
				+ SqlUtils.QuataStr(tagName);
		return this.getInt(sql);
	}

	public int updateGameTagCountClickByTagIdAndTagType(int tagId,
			GameTagType gameTagType, long countClick)
			throws ExceptionCommonBase {
		String sql = "update t_game_tag set count_click=" + countClick
				+ "where tag_id=" + tagId + "and tag_type="
				+ gameTagType.value();
		return this.executeUpdate(sql);
	}

	public void deleteGameTagIdByGameTagType(GameTagType gameTagType)
			throws ExceptionCommonBase {
		String sql = "delete from t_game_tag where tag_type = "
				+ gameTagType.value();
		this.executeUpdate(sql);
	}

	public int setGameTagInfo(GameTagInfo gameTagInfo)
			throws ExceptionCommonBase {
		int identity = 0;
		if (gameTagInfo != null && gameTagInfo.getUpdateStatus() > 0) {
			StringBuilder sb = new StringBuilder();
			sb.append("insert into t_game_tag (tag_id, tag_name, tag_type,enable, count_click,");
			sb.append("count_game, remark) values (%1$d,%2$s,%3$d,%4$d,%5$d,%6$d,%7$s) ");
			sb.append("on duplicate key update tag_name=%2$s,tag_type=%3$d,enable=%4$d,");
			sb.append("count_click=%5$d,count_game=%6$d,remark=%7$s");
			Integer tagType = (gameTagInfo.getTagType() == null)
					? null
					: gameTagInfo.getTagType().value();
			// 拼接SQL语句
			String sql = String.format(sb.toString(), gameTagInfo.getTagId(),
					SqlUtils.QuataStr(gameTagInfo.getTagName()), tagType,
					gameTagInfo.getEnable(), gameTagInfo.getCountClick(),
					gameTagInfo.getCountGame(),
					SqlUtils.QuataStr(gameTagInfo.getRemark()));
			identity = this.getIdentityId(sql);
		}
		return identity;
	}

}
